from Tkinter import *
from sgbd import *
import Pmw

class Abc( Frame ):
             
    def __init__(self, atrLivro, atrAutor):
        self.sgbd = SGBD()
        
        Frame.__init__(self)
        Pmw.initialise()
        self.pack(expand = YES, fill = BOTH)
        self.master.title("JOIN")
        
        condicoes = []        
        resultado = []
        
        self.joinAttr = []
        self.atr1 = -1
        self.atr2 = -1       
        self.framex = Frame(self)
        self.framex.pack(pady = 5)

        self.frame1 = Frame(self.framex)
        self.frame1.pack(pady = 5)

        self.labelAtrLivro = Label( self.frame1, text = "Livro:")
        self.labelAtrLivro.pack(side = LEFT, padx = 0)
        
        self.listAtrLivro = Pmw.ScrolledListBox(self.frame1, items = atrLivro, listbox_height = 9, vscrollmode = "dynamic", selectioncommand = self.addAtrLivro)
        self.listAtrLivro.pack(side = LEFT, padx = 5)

        self.labelAtrLivro = Label( self.frame1, text = "Autor:")
        self.labelAtrLivro.pack(side = LEFT, padx = 0)
        
        self.listAtrAutor = Pmw.ScrolledListBox(self.frame1, items = atrAutor, listbox_height = 9, vscrollmode = "dynamic", selectioncommand = self.addAtrAutor)
        self.listAtrAutor.pack( side = LEFT, expand = YES, fill = BOTH, padx = 5, pady = 5 )
        
        self.labelIgual = Label( self.frame1, text = "->")
        self.labelIgual.pack( side = LEFT, padx = 40, pady = 5 )
        
        self.listCond = Pmw.ScrolledListBox(self.frame1, items = condicoes, listbox_width = 40, listbox_height = 9, vscrollmode = "dynamic")
        self.listCond.pack( side = LEFT, expand = YES, fill = BOTH, padx = 5, pady = 5)
        
        self.frame2 = Frame(self.framex)
        self.frame2.pack(side= LEFT, pady = 5 )
        
        self.labelW = Label( self.frame2, text = "WHERE")
        self.labelW.pack( side = LEFT, padx = 2, pady = 5 )
        self.display = Entry(self.frame2, width = 30)
        self.display.pack(side = LEFT, padx = 5)

        self.botaoAddCond = Button(self.frame2, text = "Adicionar",command = self.adiciona )
        self.botaoAddCond.pack( side = LEFT, padx = 5, pady = 5 )
        
        self.botaoRemCond = Button(self.frame2, text = "Remove",command = self.remove )
        self.botaoRemCond.pack( side = LEFT, padx = 120, pady = 0 )
        
        self.botaoJoin = Button(self.frame2, text = "Hash Join",command = self.hashjoin )
        self.botaoJoin.pack( side = LEFT, padx = 0, pady = 5 )

        self.botaoJoin = Button(self.frame2, text = "Merge Join",command = self.mergejoin )
        self.botaoJoin.pack( side = LEFT, padx = 2, pady = 5 )
        
        self.frame3 = Frame(self)
        self.frame3.pack(side = LEFT, pady = 5)
      
        self.result = Pmw.ScrolledListBox(self.frame3, items = resultado, listbox_width = 110, listbox_height = 20, hscrollmode = "dynamic", vscrollmode = "dynamic")
        self.result.pack( side = LEFT, expand = YES, fill = BOTH, padx = 5, pady = 25 )
       
    def adiciona(self):
        self.joinAttr += [[int(self.atr1), int(self.atr2)]] 
        self.listCond.insert(END,self.display.get())
    
    def remove(self):
        try:
            i = self.listCond.get(0, 'end').index(self.listCond.getvalue()[0])
            self.joinAttr.pop(i)
            self.listCond.delete(i)
        except:
            pass
             
    def mergejoin(self):
        res = self.sgbd.mergejoin(self.joinAttr)
        txt = ["Merge join - tempo: %.30f" % res[1]]
        for l,a in res[0]:
            txt += [str(l.__repr__())+"     -     "+str(a.__repr__())]
        self.result.setlist(txt)

    def hashjoin(self):
        res = self.sgbd.hashjoin(self.joinAttr)
        txt = ["Hash join - tempo: %.30f" % res[1]]
        for l,a in res[0]:
            txt += [str(l.__repr__())+"     -     "+str(a.__repr__())]
        self.result.setlist(txt)
        
    def addAtrAutor(self):
        self.display.delete(0, len(self.display.get()))
        i = self.listAtrAutor.curselection()[0]
        self.atr2 = i
        self.display.insert(INSERT, "livro."+self.listAtrLivro.get(self.atr1) + " = " + "autor."+self.listAtrAutor.get(i))

    def addAtrLivro(self):
        self.display.delete(0, len(self.display.get()))
        i = self.listAtrLivro.curselection()[0]
        self.atr1 = i
        self.display.insert(INSERT, "livro."+self.listAtrLivro.get(i) + " = " + "autor."+self.listAtrAutor.get(self.atr2))
        

def main():
    atrLivro = ["id", "titulo", "editora", "idautor", "ano", "edicao", "numeroPaginas", "premiado", "formato"]
    atrAutor = ["id", "nome", "idade", "premiado"] 
    Abc(atrLivro, atrAutor ).mainloop()

if __name__ == "__main__":
    main()



